﻿using System;
using System.Web;
using blackhouse.BlackApart.Web.Navigation;
using blackhouse.mojoPortalExtend;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Reservations;

namespace blackhouse.StareKino.Web.Navigation {
    public class ReservationPageUrlVisitor : UrlVisitor, IReservationPageVisitor {

        #region Fields

        // ReSharper disable InconsistentNaming
        private const string R_APARTMENT_TYPE_ID = "apartment";
        private const string R_STEP = "step";
        // ReSharper restore InconsistentNaming

        #endregion

        public void Apply(ReservationPage page) {
            int siteId = 1;
            if (page.OverrideLanguageSiteId > 1)
                siteId = page.OverrideLanguageSiteId;
            var pageProv = new PageProvider();
            var fu = pageProv.FindCmsUrlByFunctionality(siteId, "reservation");
            if (fu != null && fu.FoundFriendlyUrl) {
                this.Url = PageProvider.AdjustUrlToSite(siteId, fu.Url);
                this.Url = this.Url + "?" + R_STEP + "=" + page.Type.ToString().ToLower();
                switch (page.Type) {
                    case ReservationSubpageType.AdditionalServices:
                    case ReservationSubpageType.Payment:
                    case ReservationSubpageType.Contact:
                    case ReservationSubpageType.EndReservation:
                    case ReservationSubpageType.Status:
                    case ReservationSubpageType.Confirm:
                    case ReservationSubpageType.PayReservation:
                        this.Url = this.Url + "&reservation=" + page.Reservation.Guid;
                        break;
                    case ReservationSubpageType.PreInformation:
                    case ReservationSubpageType.Information:
                        this.Url = this.Url + "&search=" + page.State + "&" + R_APARTMENT_TYPE_ID + "=" + page.ApartmentTypeId;
                        if (page.OrderingBreakfasts)
                            this.Url = this.Url + "&breakfasts=1";
                        break;
                }
            }
        }

        internal ReservationPage ReadReservationPageFromRequest() {
            HttpRequest r = HttpContext.Current.Request;
            ReservationSubpageType type;
            ReservationPage rp = null;
            if (!Enum.TryParse(r.QueryString["step"], true, out type)) return null;
            int apartmentTypeId;
            IWebReservation reservation;
            switch (type) {
                case ReservationSubpageType.PreInformation:
                    if (Int32.TryParse(r.QueryString[R_APARTMENT_TYPE_ID], out apartmentTypeId)) {
                        rp = ReservationPage.GetPreInformationPage(apartmentTypeId);
                        rp.OrderingBreakfasts = r.QueryString["breakfasts"] == "1";
                    }
                    break;
                case ReservationSubpageType.Information:
                    if (Int32.TryParse(r.QueryString[R_APARTMENT_TYPE_ID], out apartmentTypeId)) {
                        rp = ReservationPage.GetInformationPage(apartmentTypeId);
                        rp.OrderingBreakfasts = r.QueryString["breakfasts"] == "1";
                    }
                    break;
                case ReservationSubpageType.Contact:
                    reservation = this.GetReservationFromGuid(r);
                    if (reservation != null)
                        rp = ReservationPage.GetContactPage(reservation);
                    break;
                case ReservationSubpageType.AdditionalServices:
                    break;
                case ReservationSubpageType.Payment:
                    reservation = this.GetReservationFromGuid(r);
                    if (reservation != null)
                        rp = ReservationPage.GetPaymentPage(reservation);
                    break;
                case ReservationSubpageType.EndReservation:
                    reservation = this.GetReservationFromGuid(r);
                    if (reservation != null)
                        rp = ReservationPage.GetEndPage(reservation);
                    break;
                case ReservationSubpageType.Status:
                    reservation = this.GetReservationFromGuid(r);
                    if (reservation != null)
                        rp = ReservationPage.GetStatusPage(reservation);
                    break;
                case ReservationSubpageType.Confirm:
                    reservation = this.GetReservationFromGuid(r);
                    if (reservation != null)
                        rp = ReservationPage.GetConfirmationPage(reservation);
                    break;
                case ReservationSubpageType.PayReservation:
                    break;
            }
            return rp;
        }

        private IWebReservation GetReservationFromGuid(HttpRequest r) {
            string sRGuid = r.QueryString["reservation"];
            Guid rGuid;
            if (Guid.TryParse(sRGuid, out rGuid)) {
                IWebReservation reservation = Factory.Instance().GetWebReservation(rGuid);
                if (reservation.Guid.Equals(rGuid))
                    return reservation;
            }
            return null;
        }

    }
}